/* hostLib.h - public header for hostLib files */

/*
 * Copyright (c) 1984-2005 Wind River Systems, Inc.
 *
 * The right to copy, distribute or otherwise make use of this software
 * may be licensed only pursuant to the terms of an applicable Wind River
 * license agreement. No license to Wind River intellectual property rights
 * is granted herein. All rights not licensed by Wind River are reserved
 * by Wind River.
 */

/*
modification history
--------------------
02a,28jun05,vvv  updated gethostbynameInit() prototype
01z,07feb05,vvv  _KERNEL cleanup
01y,14jan05,vvv  osdep.h cleanup
01x,22sep04,dlk  Added declaration for hostTblSetup().
01w,23aug04,rp   merged from COMP_WN_IPV6_BASE6_ITER5_TO_UNIFIED_PRE_MERGE
01v,07dec03,vvv  moved definition of struct hostArgs from hostLib.c; moved
		 definition of getnameinfoArgs from getnameinfo.c
01u,24nov03,vvv  updated for RTP support
01t,20nov03,niq  Remove copyright_wrs.h file inclusion
01s,05nov03,cdw  Removal of unnecessary _KERNEL guards.
01r,04nov03,rlm  Ran batch header path update for header re-org.
01q,03nov03,rlm  Removed wrn/coreip/ prefix from #includes for header re-org.
01p,23oct03,rlm  updated #includes for header re-org
01o,06aug03,nee  Accordion Base6 merge from ACCORDION_BASE6_MERGE_BASELINE
                 label
01n,09may03,vvv  included vwModNum.h and in6.h
01m,20feb03,ann  added prototypes for gethostbyaddrInit and gethostbynameInit
01l,06sep01,hgo  modifications and extensions for IPv6
01k,20aug97,jag  added function declations for hostTblSearchByAddr(),
                 hostTblSearchByName() (SPR #9175)
01j,19may97,spm  added S_hostLib_INVALID_PARAMETER error value
01i,22sep92,rrr  added support for c++
01h,27jul92,elh  Moved hostShow to netShow.
01g,04jul92,jcf  cleaned up.
01f,26may92,rrr  the tree shuffle
01e,04oct91,rrr  passed through the ansification filter
		  -fixed #else and #endif
		  -changed VOID to void
		  -changed copyright notice
01d,05oct90,shl  added ANSI function prototypes.
                 made #endif ANSI style.
                 added copyright notice.
01c,13apr89,shl  added MAXHOSTNAMELEN.
01b,04jun88,gae  changed S_remLib_* to S_hostLib_*.
01a,28may88,dnw  extracted from remLib.h
*/

#ifndef __INChostLibh
#define __INChostLibh

#ifdef __cplusplus
extern "C" {
#endif

#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <lstLib.h>
#include <vwModNum.h>

#ifdef INET6
#include <netinet6/in6.h>
#endif /* INET6 */


#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 	64    /* XXX already defined in param.h */
#endif

/* status messages */

#define S_hostLib_UNKNOWN_HOST			(M_hostLib | 1)
#define S_hostLib_HOST_ALREADY_ENTERED		(M_hostLib | 2)
#define S_hostLib_INVALID_PARAMETER             (M_hostLib | 3)
/* former h_errno values */
#define S_hostLib_NETDB_INTERNAL                (M_hostLib | 4)  /* see errno */
/* Authoritative Answer Host not found */
#define S_hostLib_HOST_NOT_FOUND                (M_hostLib | 5)
/* Non-Authoritative Host not found, or SERVERFAIL */
#define S_hostLib_TRY_AGAIN                     (M_hostLib | 6)
/* Valid name, no data record of requested type */
#define S_hostLib_NO_RECOVERY                   (M_hostLib | 7)

#if ((CPU_FAMILY==I960) && (defined __GNUC__))
#pragma align 1                    /* tell gcc960 not to optimize alignments */
#endif  /* CPU_FAMILY==I960 */

/* Definitions for sysctl nodes under CTL_NET */

#define HOSTLIB    3
#define NAMEINFO   4

/* Options for the host library sysctl handler */

#define HOSTADD          1
#define HOSTDELETE       2
#define HOSTADDRSEARCH   3
#define HOSTNAMESEARCH   4
#define HOSTNAMESEARCH2  5

/*
 * Structure shared by hostAdd, hostDelete and hostTblSearchByName2
 * for passing arguments to sysctl.
 */

struct hostArgs
    {
    char * hostName;
    int    hostNameLen;
    char * hostAddr;
    int    hostAddrLen;
    const struct addrinfo * pHints;
    };

/*
 * This structure is used to pass the getnameinfo arguments from user-space
 * to the kernel using sysctl.
 */

struct nameinfoArgs
    {
    const struct sockaddr * sa;
    socklen_t               salen;
    char *                  host;
    size_t                  hostlen;
    char *                  serv;
    size_t                  servlen;
    int                     flags;
    };

#ifdef _WRS_KERNEL
/* typdef's for hosttable access */

typedef struct hostname_struct
    {
    struct hostname_struct *link;
    char *name;
    } HOSTNAME;

typedef struct addr4_struct
    {
    struct in_addr addr;
    } ADDR4;

#ifdef INET6
typedef struct addr6_struct
    {
    struct in6_addr addr;
    uint32_t scopeId;
    } ADDR6;
#endif

typedef union addr_union
    {
    ADDR4 addr4;
#ifdef INET6
    ADDR6 addr6;
#endif
    } ADDRU;

typedef struct
    {
    NODE node;
    HOSTNAME hostName;
    int family;                   /* AF_INET or AF_INET6 */
    ADDRU netAddr;                /* ADDR4 or ADDR6 */
    } HOSTENTRY;
#endif  /* _WRS_KERNEL */

#if ((CPU_FAMILY==I960) && (defined __GNUC__))
#pragma align 0                 /* turn off alignment requirement */
#endif  /* CPU_FAMILY==I960 */


/* function declarations */

#if defined(__STDC__) || defined(__cplusplus)

extern STATUS 	hostAdd (char *hostName, char *hostAddr);
extern STATUS 	hostDelete (char *name, char *addr);
extern STATUS 	hostGetByAddr (int addr, char *name);
extern int 	hostGetByName (char *name);
extern int      getnameinfo (const struct sockaddr *sa, socklen_t salen, char *host,
                             size_t hostlen, char *serv, size_t servlen, int flags);
extern struct hostent * hostentAlloc (void);
extern void hostentFree (struct hostent *);
extern struct hostent * gethostbyaddr (const char *addr, int len, int type);
extern struct hostent * gethostbyname (const char *name);
extern struct servent *getservbyname (const char *name, const char *proto);
extern struct servent *getservbyport (int port, const char *proto);
extern int      getaddrinfo (const char *hostname, const char *servname,
                             const struct addrinfo *hints, struct addrinfo **result);
extern void     freeaddrinfo (struct addrinfo *ai);
extern void freehostent (struct hostent *);
extern int 	gethostname (char *name, int nameLen);
extern int 	sethostname (char *name, int nameLen);
extern char    *gai_strerror (int ecode);
extern struct	 hostent *_dns_ghbyaddr(const void *addr, int addrlen, int af, int *errp);

#ifdef _WRS_KERNEL
extern STATUS 	hostTblInit (void);
extern STATUS   hostTblSetup (char * pRemoteName, char * pRemoteAddr,
			      char * pLocalName, char * pLocalAddr);
extern int      hostTblSearchByName (char *name);
extern STATUS   hostTblSearchByAddr (int addr, char *name);
extern STATUS   gethostbyaddrInit (void);
extern STATUS   gethostbynameInit (BOOL);
#endif /* _WRS_KERNEL */

#else	/* __STDC__ */

extern STATUS 	hostAdd ();
extern STATUS 	hostDelete ();
extern STATUS 	hostGetByAddr ();
extern int 	hostGetByName ();
extern int      getnameinfo ();
extern struct hostent * hostentAlloc ();
extern void hostentFree ();
extern struct hostent * gethostbyaddr ();
extern struct hostent * gethostbyname ();
extern struct servent *getservbyname ();
extern struct servent *getservbyport ();
extern int      getaddrinfo ();
extern void     freeaddrinfo ();
extern void freehostent ();
extern int 	gethostname ();
extern int 	sethostname ();
extern char    *gai_strerror ();
extern struct   hostent *_dns_ghbyaddr();

#ifdef _WRS_KERNEL
extern STATUS 	hostTblInit ();
extern STATUS   hostTblSetup ();
extern int      hostTblSearchByName ();
extern STATUS   hostTblSearchByAddr ();
extern STATUS   gethostbyaddrInit ();
extern STATUS   gethostbynameInit ();
#endif /* _WRS_KERNEL */

#endif	/* __STDC__ */

#ifdef __cplusplus
}
#endif

#endif /* __INChostLibh */
